#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 学生最基本信息 
struct INFO {
	char* name;
	int age;
	char sex;
	char addr[50];
};

// 学生基本信息 
typedef struct Student {
	struct INFO info;   // 结构体类型的变量 
	
	int id;
	float score;
}Stu;

int main()
{
    Stu* p = NULL;
    p = (Stu*)malloc(sizeof(Stu));
    if(NULL == p) {
        printf("堆区空间开辟失败\n");
        return 0;
    }

    // 初始化堆区这块空间 --> 给结构体中的成员变量赋值
    p->info.name = (char*)malloc(sizeof(char) * 50); 
    if(NULL == p->info.name) {
        printf("开辟失败\n");
        return 0;
    }

    strcpy(p->info.name, "zhang3");
    p->info.age = 15;
    p->info.sex = 'M';
    strcpy(p->info.addr, "Beijing");

    p->id = 100;
    p->score = 88.88;

    printf("name = %s, age = %d, sex = %c, addr = %s, id = %d, score = %.3f\n", p->info.name,
           p->info.age, p->info.sex, p->info.addr, p->id, p->score);  

    
    // 堆区空间，只能被销毁一次
    if(p->info.name != NULL) {
        free(p->info.name);
        p->info.name = NULL;
    }
    
    if(p != NULL) {
        free(p);
        p = NULL;
    }

    return 0;
}

